#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Hello
#include <boost/test/unit_test.hpp>

#include "Fixture.h"

#include <chrono>

int add (int a , int b) {
  return (a+b);
}
 
BOOST_AUTO_TEST_CASE(universeInOrder)
{
    BOOST_CHECK(add(2, 2) == 4);
}

/**
 * Basic test case by Creating two thread Pool and adding three Tasks
 */
BOOST_AUTO_TEST_CASE(ThreadPool1)
{
  BOOST_CHECK(Task::_threadCount == 0);

  Fixture fx("---------- ThreadPool1 --------------");
  {
    //Start two thread Pool
    ThreadPool::ThreadPool th(2);

    //Let all threads start
    std::this_thread::sleep_for(std::chrono::seconds(1));

    //Add one Task
    ThreadPool::TaskPtr ptr(new Task());
    th.addTask(std::move(ptr));

    //Let this Task be picked up by one thread from pool and execute it
    std::this_thread::sleep_for(std::chrono::seconds(1));

    //Add another task
    ThreadPool::TaskPtr ptr2(new Task());
    th.addTask(std::move(ptr2));

    std::this_thread::sleep_for(std::chrono::seconds(1));

    ThreadPool::TaskPtr ptr3(new Task());
    th.addTask(std::move(ptr3));

    //Wait till Task is finished
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }

  BOOST_CHECK(Task::_threadCount == 3);
}

/**
 * Create 10 Thread Pool and add 100 Tasks
 */
BOOST_AUTO_TEST_CASE(ThreadPool2)
{
  Task::_threadCount = 0;
  BOOST_CHECK(Task::_threadCount == 0);

  Fixture fx("--------------  ThreadPool2 --------------");
  {
    //Start two thread Pool
    ThreadPool::ThreadPool th(5);

    for (int i=0;i<100;i++) {
      //Add one Task
      ThreadPool::TaskPtr ptr(new Task());
      th.addTask(std::move(ptr));
    }

    std::this_thread::sleep_for(std::chrono::seconds(5));
  }

  BOOST_CHECK(Task::_threadCount == 100);
}
